package com.lw.sbdemo2.common.vo;

import com.lw.sbdemo2.common.exception.ResponseCodeEnum;

import java.util.Objects;

/**
 * <p>标题: 平台接口返回实体</p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2018</p>
 * <p>公司: 智业软件</p>
 *
 * @author LinWang
 * @date 2018-02-27 9:45
 */
public class ResultModel<T> {

    /**
     * 成功编码
     */
    public static final String SUCCESS_CODE = ResponseCodeEnum.SUCCESS.getCode();

    /**
     * 异常编码
     */
    public static final String ERROR_CODE = ResponseCodeEnum.EX_ERROR.getCode();

    /**
     * 响应编码
     */
    private String code = SUCCESS_CODE;

    /**
     * 响应数据
     */
    private T data;

    /**
     * 响应信息
     */
    private String message = "";

    /**
     * 异常详细信息
     */
    private String error = "";

    /**
     * 返回成功
     * @param data
     * @param <T>
     * @return
     */
    public static <T> ResultModel<T> success(T data) {
        return success(data, "");
    }

    /**
     * 返回成功
     * @param data
     * @param message
     * @param <T>
     * @return
     */
    public static <T> ResultModel<T> success(T data, String message) {
        return new ResultModel(SUCCESS_CODE, data, message);
    }

    /**
     * 返回失败
     * @param code
     * @param message
     * @param error
     * @return
     */
    public static ResultModel fail(String code, String message, String error) {
        return new ResultModel(code, null, message, error);
    }

    public static ResultModel fail(ResponseCodeEnum code) {
        return new ResultModel(code.getCode(), null, code.getMessage(), code.getMessage());
    }

    public static ResultModel fail(ResponseCodeEnum code, String error) {
        return new ResultModel(code.getCode(), null, code.getMessage(), error);
    }

    public static ResultModel fail(String error) {
        return new ResultModel(ResponseCodeEnum.EX_ERROR.getCode(), null, error, error);
    }

    public boolean isSuccess() {
        return Objects.equals(this.code, ResponseCodeEnum.SUCCESS.getCode());
    }

    public ResultModel() {
    }

    public ResultModel(String code, T data, String message) {
        this.code = code;
        this.data = data;
        this.message = message;
    }

    public ResultModel(String code, T data, String message, String error) {
        this.code = code;
        this.data = data;
        this.message = message;
        this.error = error;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }
}
